/*
 * @(#)IpRouteParser.java 2008-7-8
 * 
 * Copyright 5jxiang . All rights reserved.
 */
package com.jz.util;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * <p>
 * <a href="IpRouteParser.java.html"><i>View Source</i></a>
 * </p>
 *
 * @author 5jxiang
 * @version $Id$
 */
public class IpRoutes {
	public static List<Long>[] ips;
	public static void initIpRoutes(String filePath) {
		InputStream stream = null;
		ips = new List[23];
		 try {
	        	stream = new FileInputStream(filePath);
	        	BufferedReader dr=new BufferedReader(new InputStreamReader(stream));
	        	String line =  dr.readLine();
	        	while(line!= null){ 
	        		int i0 = line.indexOf("=");
	        		int i1 = line.indexOf("/");
        			int i2 = line.indexOf("gateway");
	        		if (i0 > 0 && i1 > i0 && i2 > i1) {
	        			String ipStr = line.substring(i0 + 1, i1);
	        			String bitCountStr = line.substring(i1 + 1, i2).trim();
	        			long ipvalue = ipToLong(ipStr);
	        			int bitCountValue = Integer.parseInt(bitCountStr);
	        			if (bitCountValue > 0) {
	        				ipvalue = ipvalue >> (32 - bitCountValue);
	        				List<Long> list = ips[32 - bitCountValue];
	        				if (list == null) {
	        					list = new ArrayList<Long>();
	        					ips[32 - bitCountValue] = list;
	        				}
	        				list.add(ipvalue);
	        			}
	        			
	        		}
	        		line = dr.readLine();
	        	} 
	        	
	        	for (int i = 0; i < ips.length; i++) {
	        		List list = ips[i];
	        		if (list != null) {
	        			Collections.sort(list);
	        		}
	        	}
	        
	        	
	        } catch (IOException e) {
	            throw new RuntimeException("Could not load " + filePath + e);
	        } finally {
	        	if (stream != null) {
	        		try {
						stream.close();
					} catch (IOException e) {}
	        	}
	        }
	}
	
	public static boolean isCNCIp(String ipAddress) {
		if (ipAddress == null ||ipAddress.length() < 7 || ips == null || ips.length < 1) return false;
		long ip = ipToLong(ipAddress);
		for(int i = 0; i < ips.length; i++) {
			if (ips[i] != null) {
				long p = ip >> (32 - i);
				int low = 0;
		    	int high = ips[i].size()-1;
		
		    	while (low <= high) {
		    	    int mid = (low + high) >> 1;
		    	    long midVal = ips[i].get(mid);
		    	    if (p > midVal) {
		    	    	low = mid + 1;
		    	    } else if (p < midVal) {
		    	    	high = mid - 1;
		    	    } else {
		    	    	return true;
		    	    }
		    	}
			}
		}
		return false;
	}
	
	    
	    //将127.0.0.1形式的IP地址转换成十进制整数，这里没有进行任何错误处理 
	    public static long ipToLong(String strIp) { 
	        long[] ip = new long[4]; 
	        //先找到IP地址字符串中.的位置 
	        int position1 = strIp.indexOf("."); 
	        int position2 = strIp.indexOf(".", position1 + 1); 
	        int position3 = strIp.indexOf(".", position2 + 1); 
	        //将每个.之间的字符串转换成整型 
	        ip[0] = Long.parseLong(strIp.substring(0, position1)); 
	        ip[1] = Long.parseLong(strIp.substring(position1+1, position2)); 
	        ip[2] = Long.parseLong(strIp.substring(position2+1, position3)); 
	        ip[3] = Long.parseLong(strIp.substring(position3+1)); 
	        return (ip[0] << 24) + (ip[1] << 16) + (ip[2] <<  8 ) + ip[3]; 
	    } 
	    
	    //将十进制整数形式转换成127.0.0.1形式的ip地址 
	    public static String intToIP(long longIp){ 
	        StringBuffer sb = new StringBuffer(""); 
	        //直接右移24位 
	        sb.append(String.valueOf((longIp >>> 24))); 
	        sb.append("."); 
	        //将高8位置0，然后右移16位 
	        sb.append(String.valueOf((longIp & 0x00FFFFFF) >>> 16)); 
	        sb.append("."); 
	        //将高16位置0，然后右移8位 
	        sb.append(String.valueOf((longIp & 0x0000FFFF) >>>  8 )); 
	        sb.append("."); 
	        //将高24位置0 
	        sb.append(String.valueOf((longIp & 0x000000FF))); 
	        return sb.toString(); 
	   }
	  
}
